﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BarberShop.Model;
using BarberShop.Data;
using System.Linq.Expressions;
using BarberShop.Common;

namespace BarberShop.BLL
{
    public class MovePlayListRelationBLL
    {

        public static IList<MovePlayListRelation> GetMoviePalyListRe(int MovePlayListInfo_ID)
        { 
            using(DataClassesDataContext data = new DataClassesDataContext())
            {
                Expression<Func<MovePlayListRelation, bool>> WhereExpr = PredicateExtensions.True<MovePlayListRelation>();
                if (MovePlayListInfo_ID != -1)
                {
                    WhereExpr = WhereExpr.And(x => x.MovePlayListInfo_ID.Equals(MovePlayListInfo_ID));
                }
                var Move = data.MovePlayListRelation.Where(WhereExpr);
                return Move.ToList();
            }
        }

        public static IList<MoveInfo> GetMoviePalyListMoveInfo(int MovePlayListInfo_ID)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                Expression<Func<MovePlayListRelation, bool>> WhereExpr = PredicateExtensions.True<MovePlayListRelation>();
                if (MovePlayListInfo_ID != -1)
                {
                    WhereExpr = WhereExpr.And(x => x.MovePlayListInfo_ID.Equals(MovePlayListInfo_ID));
                }
                var Move = data.MovePlayListRelation.Where(WhereExpr).OrderBy(r=>r.PlayIndex);
                IList<MoveInfo> list = new List<MoveInfo>();
                foreach (var item in Move)
                {
                    var movedata=MovieInfoBLL.GetMoveInfo(item.MoveInfo_ID.Value);
                    list.Add(movedata);

                }
                return list;
            }
        }

        public static bool SavePalyList(int id,string pids,string mids)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    var playlist = data.MovePlayListRelation.Where(r => r.MovePlayListInfo_ID == id).ToList();
                    data.MovePlayListRelation.DeleteAllOnSubmit(playlist);
                    var ids_p = pids.Split(',');
                    var ids_m = mids.Split(',');
                    if (ids_p.Count() > 0 && ids_m.Count() > 0)
                    {
                        MovePlayListRelation m = null;
                        for (int i = 0; i < ids_m.Length; i++)
                        {
                            if (ids_p[i] != "" && ids_p[i] != ",")
                            {
                                m = new MovePlayListRelation();
                                m.MovePlayListInfo_ID = id;
                                m.PlayIndex = int.Parse(ids_p[i]);
                                m.MoveInfo_ID = int.Parse(ids_m[i]);
                                data.MovePlayListRelation.InsertOnSubmit(m);
                            }
                        }
                    }
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
                
            }
        }

        public static bool AddPalyList(int id, string mids)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    var playlist = data.MovePlayListRelation.Where(r => r.MovePlayListInfo_ID == id).ToList();
                    var ids_m = mids.Split(',');
                    if (ids_m.Count() > 0)
                    {
                        MovePlayListRelation m = null;
                        int maxindex = GetMaxPlayindex(id);
                        for (int i = 0; i < ids_m.Length; i++)
                        {
                            if (ids_m[i] != "," && ids_m[i] != "")
                            {
                                m = new MovePlayListRelation();
                                m.MovePlayListInfo_ID = id;
                                m.PlayIndex = maxindex+1;
                                m.MoveInfo_ID = int.Parse(ids_m[i]);
                                data.MovePlayListRelation.InsertOnSubmit(m);
                                maxindex++;
                            }
                        }
                    }
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }

            }
        }
        public static int GetMaxPlayindex(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var index = (from a in data.MovePlayListRelation where a.MovePlayListInfo_ID == id select a.PlayIndex);
                if (index.ToList().Count == 0)
                    return 0;
                return index.Max(r=>r.Value);
            }
        }
        /// <summary>
        /// 添加影片
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool AddMovePlayListReInfo(MovePlayListRelation info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    data.MovePlayListRelation.InsertOnSubmit(info);
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }

        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static MovePlayListRelation GetMovePlayListReInfo(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var info = (from a in data.MovePlayListRelation where a.ID == id select a).FirstOrDefault();
                return info;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pid">播放列表ID</param>
        /// <param name="mid">影片ID</param>
        /// <returns></returns>
        public static bool DelPlayList(int pid,int mid)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    var info = (from a in data.MovePlayListRelation where a.MovePlayListInfo_ID == pid && a.MoveInfo_ID == mid select a).FirstOrDefault();
                    if (info != null)
                    {
                        data.MovePlayListRelation.DeleteOnSubmit(info);
                        data.SubmitChanges();
                        return true;
                    }
                    else { return false; }
                }
                catch { return false; }
            }
        }

        public static bool DelPlayList(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    var info = (from a in data.MovePlayListRelation where a.ID==id select a).FirstOrDefault();
                    if (info != null)
                    {
                        data.MovePlayListRelation.DeleteOnSubmit(info);
                        data.SubmitChanges();
                        return true;
                    }
                    else { return false; }
                }
                catch { return false; }
            }
        }

        public static bool UpdateMovePlayListReInfo(MovePlayListRelation info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    var r = (from a in data.MovePlayListRelation where a.ID == info.ID select a).FirstOrDefault();
                    r.ID = info.ID;
                    r.MoveInfo_ID = info.MoveInfo_ID;
                    r.MovePlayListInfo_ID = info.MovePlayListInfo_ID;
                    r.PlayIndex = info.PlayIndex;
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }
        }
    }
}
